#! /usr/bin/python
# -*- coding:utf-8 -*-

## ========================================================================
## Copyright (C) 2007-2013  Jean-Philippe GOLAY
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
## ========================================================================

from persistance import *

class User:
  
  def __init__(self):
    self.mDB=ManageDB(Glob.dbName, Glob.user, Glob.passwd, Glob.host)
    logging.basicConfig(filename=Glob.LOG_FILENAME,level=logging.DEBUG)


  def checkUser(self, username, passwd):
    if (self.mDB.executSQL(Glob.select_id_from_user_where_passwd % passwd) == 0):
      sql=Glob.select_id_from_user_where_passwd % passwd
      logging.debug(u'User: checkUser() SQL error : %s' % sql)
    else:
      result=self.mDB.resultSQL()
      if len(result) == 0:
        return 0
      else:
        for row in result:
          self.usr_id=row[0]
    if self.usr_id:
      return 1
    else:
      return 0


  def getID(self):
    if self.usr_id:
      return self.usr_id
    else:
      return -1


  def new(self, username, passwd):
    u_username=unicode(username, u'utf8')
    u_passwd=unicode(passwd, u'utf8')
    if (self.mDB.executSQL(Glob.insert_user % (u_username, u_passwd) ) == 0):
      sql=Glob.insert_user % (u_username, u_passwd)
      logging.debug(u'User: new() SQL error : %s' % sql)
      return 0
    else:
      if (self.mDB.executSQL(Glob.last_insert_id_user ) == 0):
        logging.debug(u'User: new() SQL error : %s' % Glob.last_insert_id_user)
        return 0
      else:
        result=self.mDB.resultSQL()
        for row in result:
          id=row[0]
        if id:
          return id 
        else:
          return 0


  def update(self, id, username, passwd):
    u_username=unicode(username, u'utf8')
    u_passwd=unicode(passwd, u'utf8')
    if (self.mDB.executSQL(Glob.update_user_where_id % (u_username, u_passwd, id) ) == 0):
      sql=Glob.update_user_where_id % (u_username, u_passwd, id)
      logging.debug(u'User: update() SQL error : %s' % sql)
      return 0
    else:
      return 1


  def delete(self, id):
    if (self.mDB.executSQL(Glob.delete_user_where_id % id) == 0):
      sql=Glob.delete_user_where_id % id
      logging.debug(u'User: delete() SQL error : %s' % sql)
      return 0
    else:
      return 1


  def read(self, id):
    if (self.mDB.executSQL(Glob.select_all_from_user_where_id % id) == 0):
      sql=Glob.select_all_from_user_where_id % id
      logging.debug(u'User: read() SQL error : %s' % sql)
      return 0
    else:
      result=self.mDB.resultSQL()
      for row in result:
        username=row[1]
        passwd=row[2]
      return { u'username': username, u'passwd': passwd }


  def getUsersList(self):
    html_list = u"<tr class='headerList'>\n<td><b>Utilisateurs</b></td>\n<td colspan='2'><b>Actions</b></td>\n</tr>\n"
    if (self.mDB.executSQL(Glob.select_all_from_user) == 0):
      logging.debug(u'User: getUsersList() SQL error : %s' % Glob.select_all_from_user)
      return html_list
    else:
      result=self.mDB.resultSQL()
      for row in result:
        html_list += u"<tr>\n<td><b>%s</b>&nbsp;&nbsp;(ID:%s)</td>\n<td><a href'#' id='%s' onclick='modifyUser(this.id)'><img src='img/edit.gif' width='16' height='16' title='Modifier'></a></td>\n<td><a href'#' id='%s' onclick='deleteUser(this.id)'><img src='img/delete_icon.gif' width='17' height='16' title='Supprimer'></a></td>\n</tr>" % (row[1], row[0], row[0], row[0])
      return html_list
     

  def getUsersHeader(self):
    html_header = u"<br/><h3>Liste des utilisateurs&nbsp;&nbsp;<a href='#' onclick='newUser()' ><img src='img/add_icon.gif' title='Nouvel utilisateur' width='16' height='16' ></a></h3><br/><table border='0' cellspacing='0' cellpadding='0' align='center' width='100%' class='users_list'>\n"
    return html_header


  def getUsersFooter(self):
    html_footer = u"</table><br/>\n"
    return html_footer
